﻿Imports System.Windows.Controls
Imports System.Windows
Imports System.Windows.Data
Imports Actuariat.Comptes
Imports System.Windows.Media

Public Class GrilleComptes
    Inherits DataGrid

#Region "Constructeur"
    Private Property onInit As Boolean = True
    Public Sub New()
        ' Cet appel est requis par le concepteur.
        InitializeComponent()
        onInit = False
    End Sub
#End Region

#Region "Objet métier contenant les éléments du compte"
    Private _ValuesComptes As ComptesMoteur2
    Public Overridable Property ValuesComptes As ComptesMoteur2
        Get
            Return GetValue(ValuesComptesProperty)
        End Get
        Set(ByVal value As ComptesMoteur2)
            If Not ValuesComptes Is Nothing Then RemoveHandler ValuesComptes.PremiereAnneeProjectionChanged, AddressOf PremiereAnneeGenerationChanged
            SetValue(ValuesComptesProperty, value)
        End Set
    End Property
    Public Shared ReadOnly ValuesComptesProperty As DependencyProperty = _
        DependencyProperty.Register("ValuesComptes", GetType(ComptesMoteur2), GetType(GrilleComptes), New FrameworkPropertyMetadata)

    Private Sub GrilleComptes_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.CurrentCellChanged

    End Sub
    Private Sub GrilleComptes_DataContextChanged(ByVal sender As Object, ByVal e As System.Windows.DependencyPropertyChangedEventArgs) Handles Me.DataContextChanged
        MAJ_DataContextComptes()
    End Sub
    Private Sub MAJ_DataContextComptes()
        If TypeOf Me.DataContext Is VMGrilleCompte And Not Me.DataContext Is Nothing Then
            Dim VM As VMGrilleCompte = DirectCast(Me.DataContext, VMGrilleCompte)
            Dim comptes As ComptesMoteur2 = ValuesComptes
            VM.Comptes = comptes
        End If
    End Sub
#End Region

    Protected Overrides Sub OnPropertyChanged(ByVal e As System.Windows.DependencyPropertyChangedEventArgs)
        MyBase.OnPropertyChanged(e)
        Select Case e.Property.Name
            Case "ValuesComptes"
                MAJ_DataContextComptes()
                AddHandler ValuesComptes.PremiereAnneeProjectionChanged, AddressOf PremiereAnneeGenerationChanged
        End Select
    End Sub

    Private Sub PremiereAnneeGenerationChanged(ByVal sender As Object, ByVal e As EventArgs)
        With CType(Me, DataGrid)
            For i = 0 To .Columns.Count - 1
                .Columns(i).Header = ValuesComptes.AnneePremiereGeneration + i
            Next
        End With
    End Sub

    Private Sub GrilleComptes_AutoGeneratingColumn(ByVal sender As Object, ByVal e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs) Handles Me.AutoGeneratingColumn
        If e.Column.Header = "ComptesHeaders" Then e.Cancel = True
        Dim txtCol As DataGridTextColumn = TryCast(e.Column, DataGridTextColumn)
        'txtCol.Binding.StringFormat = "#,##0"
        txtCol.ElementStyle = Me.FindResource("Alignment")
        If txtCol.Header <> "ComptesHeaders" Then
            txtCol.Header = ValuesComptes.AnneePremiereGeneration + CInt(txtCol.Header)
        End If
    End Sub

End Class
